{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Collecting gensim\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8a/6f/a690547cb7089d4019465bfbfbbb8bea5b3e52969cd2d6005049e6678ec4/gensim-4.2.0-cp37-cp37m-win_amd64.whl (24.0 MB)\n",
      "     ---------------------------------------- 24.0/24.0 MB 4.7 MB/s eta 0:00:00\n",
      "Collecting numpy>=1.17.0 (from gensim)\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/97/9f/da37cc4a188a1d5d203d65ab28d6504e17594b5342e0c1dc5610ee6f4535/numpy-1.21.6-cp37-cp37m-win_amd64.whl (14.0 MB)\n",
      "     ---------------------------------------- 14.0/14.0 MB 9.1 MB/s eta 0:00:00\n",
      "Requirement already satisfied: scipy>=0.18.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from gensim) (1.1.0)\n",
      "Collecting smart-open>=1.8.1 (from gensim)\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7a/18/9a8d9f01957aa1f8bbc5676d54c2e33102d247e146c1a3679d3bd5cc2e3a/smart_open-7.1.0-py3-none-any.whl (61 kB)\n",
      "     ---------------------------------------- 61.7/61.7 kB 3.2 MB/s eta 0:00:00\n",
      "Collecting Cython==0.29.28 (from gensim)\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9f/79/311cfbca90332ab37ef8ea08f1af3266f20a9a0e7a1d652842db832226bb/Cython-0.29.28-py2.py3-none-any.whl (983 kB)\n",
      "     ------------------------------------- 983.8/983.8 kB 10.4 MB/s eta 0:00:00\n",
      "Requirement already satisfied: wrapt in c:\\programdata\\anaconda3\\lib\\site-packages (from smart-open>=1.8.1->gensim) (1.10.11)\n",
      "Installing collected packages: smart-open, numpy, Cython, gensim\n",
      "  Attempting uninstall: numpy\n",
      "    Found existing installation: numpy 1.15.1\n",
      "    Uninstalling numpy-1.15.1:\n",
      "      Successfully uninstalled numpy-1.15.1\n",
      "  Attempting uninstall: Cython\n",
      "    Found existing installation: Cython 0.28.5\n",
      "    Uninstalling Cython-0.28.5:\n",
      "      Successfully uninstalled Cython-0.28.5\n",
      "Successfully installed Cython-0.29.28 gensim-4.2.0 numpy-1.21.6 smart-open-7.1.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "DEPRECATION: pandas 0.23.4 has a non-standard dependency specifier pytz>=2011k. pip 24.1 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pandas or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\n"
     ]
    }
   ],
   "source": [
    "!pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gensim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'周瑜'的词向量为：\n",
      " [-0.10756627  0.17900449  0.2937823   0.5920257  -0.4591717   0.39160517\n",
      "  0.38153192 -0.01908929 -1.1449753   0.50057036  0.07330584 -0.8200057\n",
      " -0.33220536 -0.60141736 -0.06021537 -0.11047265  0.11952201 -0.00539461\n",
      " -1.0505021  -1.0481296 ]\n",
      "与'周瑜'相似度最高的10个词：\n",
      "[('袁术', 0.9298132061958313), ('钟会', 0.9168984889984131), ('吕布', 0.9164904952049255), ('孙权', 0.9149749279022217), ('孙策', 0.9139015674591064), ('孙夫人', 0.9116554260253906), ('陆逊', 0.9114538431167603), ('袁绍', 0.9093539714813232), ('夏侯楙', 0.9075954556465149), ('鲁肃', 0.9029998779296875)]\n",
      "'刘备'和'曹操'的相似度：0.8211581707000732\n",
      "在词'孙权/曹操/刘备/刘夫人'中，'孙夫人'与其他词不属于同一类\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "import re\n",
    "from gensim.models import Word2Vec\n",
    "#读取数据\n",
    "with open(r\"C:/Users/Administrator/Desktop/sanguo.txt\",encoding='utf-8') as f:\n",
    "    lines=[]\n",
    "    for line in f:\n",
    "        temp = jieba.lcut(line) #使用jieba进行分词\n",
    "        words = []\n",
    "        for i in temp:\n",
    "            i = re.sub(\"[\\s+\\.\\!\\/_,$%^*(+\\\"\\'””《》]+|[+——！，。？、~@#￥%……&*（）：；‘]+\", \"\", i)  #删除所有的标点符号\n",
    "            if len(i) > 0:\n",
    "                words.append(i)\n",
    "        if len(words) > 0:\n",
    "            lines.append(words)\n",
    "model = Word2Vec(lines, vector_size=20, window=2, min_count=3, epochs=7, negative=10,sg=1)   #训练模型\n",
    "print(\"'周瑜'的词向量为：\\n\",model.wv.get_vector('周瑜'))\n",
    "print(\"与'周瑜'相似度最高的10个词：\")\n",
    "print(model.wv.most_similar('周瑜',topn=10))\n",
    "print(\"'刘备'和'曹操'的相似度：{}\".format(model.wv.similarity('刘备','曹操')))\n",
    "words=\"孙权 曹操 刘备 孙夫人\"\n",
    "print(\"在词'孙权/曹操/刘备/刘夫人'中，'{}'与其他词不属于同一类\".format(model.wv.doesnt_match(words.split())))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
